###########################################################################
#
# Public Procedure Header - as this procedure is modified, please be sure
#                           that you update this header block. Thanks.
#
#>>BEGIN PUBLIC<<
#
# Procedure Name : FirstTimeInitialization
#
# Description : Gather required information when used for the first time.
#
# Arguments :   None
#
# Returns :     Nothing
#
# Side-Effects :        None
#
# Exception Conditions :        None
#
# Pre-requisite Conditions :    None
#
# Original Author : Gerald W. Lester
#
#>>END PUBLIC<<
#
# Maintenance History - as this file is modified, please be sure that you
#                       update this segment of the file header block by
#                       adding a complete entry at the bottom of the list.
#
# Version     Date     Programmer   Comments / Changes / Reasons
# -------  ----------  ----------   -------------------------------------------
#       1  12/21/2009  G.Lester     Initial version
###########################################################################
proc FirstTimeInitialization {} {
    global firstTimeStatus
    global saveFlag

    set ::initValues(altapoint,lastRec) 0

    wm withdraw .
    wm deiconify .firsttime
    set firstTimeStatus 0
    set saveFlag 1
    update idletasks
    tkwait visibility .firsttime
    if {$firstTimeStatus == -1} {
        destory .
    }
}


###########################################################################
#
# Public Procedure Header - as this procedure is modified, please be sure
#                           that you update this header block. Thanks.
#
#>>BEGIN PUBLIC<<
#
# Procedure Name : CrateFirstTimeWindow
#
# Description : Create the toplevel window to gather the initialization information
#
# Arguments :   None
#
# Returns :     Nothing
#
# Side-Effects :        None
#
# Exception Conditions :        None
#
# Pre-requisite Conditions :    None
#
# Original Author : Gerald W. Lester
#
#>>END PUBLIC<<
#
# Maintenance History - as this file is modified, please be sure that you
#                       update this segment of the file header block by
#                       adding a complete entry at the bottom of the list.
#
# Version     Date     Programmer   Comments / Changes / Reasons
# -------  ----------  ----------   -------------------------------------------
#       1  12/21/2009  G.Lester     Initial version
###########################################################################
proc CrateFirstTimeWindow {} {
    set f .firsttime
    toplevel $f \
        -menu $f.menu
    wm title $f [::msgcat::mc FirstTime.Title]
    set exitLabel [::msgcat::mc FirstTime.Menu.File.Exit_Label]
    set saveLabel [::msgcat::mc FirstTime.Menu.File.Save_Label]
    wm protocol $f WM_DELETE_WINDOW [list $f.buttons.exit invoke ]
    wm withdraw $f

    ##
    ## Configure main menu (aka menu bar)
    ##
    menu $f.menu
    foreach item {File Help} {
        set menu [string tolower $f.menu.$item]
        menu $menu
        $f.menu add cascade \
            -label [::msgcat::mc FirstTime.Menu.${item}_Label] \
            -underline [::msgcat::mc FirstTime.Menu.${item}_Underline]  \
            -menu $menu
    }

    ##
    ## Configure the File Menu
    ##
    $f.menu.file add command \
        -command {SaveInializationInfo} \
        -label $saveLabel \
        -underline [::msgcat::mc FirstTime.Menu.File.Save_Underline]
    $f.menu.file add separator
    $f.menu.file add command \
        -command [list $f.buttons.exit invoke ] \
        -label $exitLabel \
        -underline [::msgcat::mc FirstTime.Menu.File.Exit_Underline]

    ##
    ## Configure the Help Menu
    ##
    $f.menu.help add command \
        -command {DisplayHelp about} \
        -label [::msgcat::mc FirstTime.Menu.Help.About_Label] \
        -underline [::msgcat::mc FirstTime.Menu.Help.About_Underline]
    $f.menu.help add separator
    $f.menu.help add command \
        -command {DisplayHelp settings} \
        -label [::msgcat::mc FirstTime.Menu.Help.TOC_Label] \
        -underline [::msgcat::mc FirstTime.Menu.Help.TOC_Underline]


    ::ttk::notebook $f.nb
    CreateInitPane
    $f.nb add $f.nb.initfile \
        -sticky nsew \
        -text [::msgcat::mc FirstTime.InitPane.Title]
    CreateFreshBookPane
    $f.nb add $f.nb.freshbooks \
        -sticky nsew \
        -text [::msgcat::mc FirstTime.FreshBookPane.Title]

    ::ttk::frame $f.buttons
    ::ttk::button $f.buttons.save \
        -command {
            puts {in firstitme save}
            set firstTimeStatus 1
            SaveInializationInfo
        } \
        -text $saveLabel
    ::ttk::button $f.buttons.exit \
        -command {
            puts {in firstitme exit}
            wm withdraw .firsttime
            set firstTimeStatus -1
        } \
        -text $exitLabel
    grid configure x $f.buttons.save x $f.buttons.exit $f.buttons.save -sticky ew
    grid columnconfigure $f.buttons {0 2 4} -weight 1

    grid configure $f.nb -sticky nsew
    grid configure $f.buttons -sticky eew
    grid rowconfigure $f $f.nb -weight 1
    grid columnconfigure $f $f.nb -weight 1

}

###########################################################################
#
# Public Procedure Header - as this procedure is modified, please be sure
#                           that you update this header block. Thanks.
#
#>>BEGIN PUBLIC<<
#
# Procedure Name : CreateInitPane
#
# Description : Create the pane for specifing where to save the inialization data
#
# Arguments :   None
#
# Returns :     Nothing
#
# Side-Effects :        None
#
# Exception Conditions :        None
#
# Pre-requisite Conditions :    None
#
# Original Author : Gerald W. Lester
#
#>>END PUBLIC<<
#
# Maintenance History - as this file is modified, please be sure that you
#                       update this segment of the file header block by
#                       adding a complete entry at the bottom of the list.
#
# Version     Date     Programmer   Comments / Changes / Reasons
# -------  ----------  ----------   -------------------------------------------
#       1  12/21/2009  G.Lester     Initial version
###########################################################################
proc CreateInitPane {} {
    global dirList
    global initValues

    foreach key {User Pwd Install skip} value $dirList {
        set initValues(dirType,$key) $value
    }
    set initValues(dirType) Install

    set f .firsttime.nb.initfile
    ::ttk::frame $f
    foreach key {User Pwd Install} {
        ::ttk::radiobutton $f.rb_$key \
            -text [::msgcat::mc FirstTime.InitPane.$key.Title] \
            -variable ::initValues(dirType) \
            -value $key
        ::ttk::entry $f.label_$key \
            -state readonly \
            -textvariable ::initValues(dirType,$key)
        grid configure $f.rb_$key $f.label_$key -sticky ew
    }
    grid columnconfigure $f $f.label_$key -weight 1

}

###########################################################################
#
# Public Procedure Header - as this procedure is modified, please be sure
#                           that you update this header block. Thanks.
#
#>>BEGIN PUBLIC<<
#
# Procedure Name : CreateFreshBookPane
#
# Description : Create the pane to gather the information to connect to FreshBooks
#
# Arguments :   None
#
# Returns :     Nothing
#
# Side-Effects :        None
#
# Exception Conditions :        None
#
# Pre-requisite Conditions :    None
#
# Original Author : Gerald W. Lester
#
#>>END PUBLIC<<
#
# Maintenance History - as this file is modified, please be sure that you
#                       update this segment of the file header block by
#                       adding a complete entry at the bottom of the list.
#
# Version     Date     Programmer   Comments / Changes / Reasons
# -------  ----------  ----------   -------------------------------------------
#       1  12/21/2009  G.Lester     Initial version
###########################################################################
proc CreateFreshBookPane {} {
    set f .firsttime.nb.freshbooks
    ::ttk::frame $f

    foreach key {Url Token} {
        ::ttk::label $f.label_$key \
            -text [::msgcat::mc FirstTime.FreshBookPane.${key}.Title]
        ::ttk::entry $f.entry_$key \
            -textvariable ::initValues(freshbooks,$key)
        grid configure $f.label_$key $f.entry_$key -sticky ew
    }
    grid columnconfigure $f $f.entry_$key -weight 1
}


###########################################################################
#
# Public Procedure Header - as this procedure is modified, please be sure
#                           that you update this header block. Thanks.
#
#>>BEGIN PUBLIC<<
#
# Procedure Name : SaveInializationInfo
#
# Description : Save the initialization information
#
# Arguments :   None
#
# Returns :     Nothing
#
# Side-Effects :        None
#
# Exception Conditions :        None
#
# Pre-requisite Conditions :    None
#
# Original Author : Gerald W. Lester
#
#>>END PUBLIC<<
#
# Maintenance History - as this file is modified, please be sure that you
#                       update this segment of the file header block by
#                       adding a complete entry at the bottom of the list.
#
# Version     Date     Programmer   Comments / Changes / Reasons
# -------  ----------  ----------   -------------------------------------------
#       1  12/21/2009  G.Lester     Initial version
###########################################################################
proc SaveInializationInfo {} {
    global initValues
    global configuration
    global saveFlag

    set dir $initValues(dirType,$initValues(dirType))
    set fd [::ini::open [file join $dir FreshBooksSynch.ini] w]

    ::ini::set $fd Init Location $initValues(dirType)

    ::ini::set $fd Connection AuthenticationToken $initValues(freshbooks,Token)
    ::ini::set $fd Connection URL $initValues(freshbooks,Url)

    ::ini::set $fd DataBase DataBaseDirectory $initValues(altapoint,dbDir)
    ::ini::set $fd DataBase DataExtension dbf
    ::ini::set $fd DataBase MemoExtension fpt
    ::ini::set $fd DataBase ClientFile Data03
    ::ini::set $fd DataBase ContactFile Data04.dbf
    ::ini::set $fd DataBase BillingCodeFile Data10
    ::ini::set $fd DataBase LastInvoiceRecordSent $initValues(altapoint,lastRec)
    ::ini::set $fd DataBase InvoiceFile Data11
    ::ini::set $fd DataBase InvoiceDetailFile Data12

    ::ini::set $fd Gui DisplayInvoiceColumns {Client Date Charges Balance}

    set ::url $initValues(freshbooks,Url)
    set ::token $initValues(freshbooks,Token)
    foreach section [::ini::sections $fd] {
        foreach {key value} [::ini::get $fd $section] {
            set configuration($section,$key) [string trim $value]
        }
    }

    ::ini::commit $fd

    SetDisplayedColumns
    if {$saveFlag} {
        LoadMainWindow
    }

    if {![info exists configuration(Mappings)]} {
        CreateDefaultMappings
    }

    foreach {item} [.firsttime.nb.mapping.list.data children {}] {
        set value [.firsttime.nb.mapping.list.data item $item -values]
        set code [lindex $value 0]
        set configuration(Mappings,$code) $value
        ::ini::set $fd Mappings $code $value
    }
    set configuration(Mappings) 1

    ::ini::commit $fd
    ::ini::close $fd

    wm withdraw .firsttime
    wm deiconify .
}

###########################################################################
#
# Public Procedure Header - as this procedure is modified, please be sure
#                           that you update this header block. Thanks.
#
#>>BEGIN PUBLIC<<
#
# Procedure Name : DisplaySettings
#
# Description : Display the initialization information
#
# Arguments :   None
\#
# Returns :     Nothing
#
# Side-Effects :        None
#
# Exception Conditions :        None
#
# Pre-requisite Conditions :    None
#
# Original Author : Gerald W. Lester
#
#>>END PUBLIC<<
#
# Maintenance History - as this file is modified, please be sure that you
#                       update this segment of the file header block by
#                       adding a complete entry at the bottom of the list.
#
# Version     Date     Programmer   Comments / Changes / Reasons
# -------  ----------  ----------   -------------------------------------------
#       1  12/21/2009  G.Lester     Initial version
###########################################################################
proc DisplaySettings {} {
    global initValues
    global configuration
    global firstTimeStatus
    global saveFlag

    set firstTimeStatus 0
    set saveFlag 0
    set f .firsttime
    set configuration(Mappings) 1
    $f.buttons.exit configure \
        -text [::msgcat::mc FirstTime.Button.Cancel.Title] \
        -command {
            wm withdraw .firsttime
            wm deiconify .
        }

    ##
    ## Populate the Ini File Section
    ##
    set initValues(dirType) $configuration(Init,Location)

    ##
    ## Populate the FreshBooks section
    ##
    set initValues(freshbooks,Token) $configuration(Connection,AuthenticationToken)
    set initValues(freshbooks,Url) $configuration(Connection,URL)

    wm deiconify $f
    after 2 [list raise $f]

}
